package sdfi.lee.DAO;


import java.io.File;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import sdfi.lee.Entity.News;
import sdfi.lee.Entity.Newspic;

public class NewsDAO {
	public String addNews(News news)
	{
		Session session=DAO.openSession();
		Transaction ts=null;
		try
		{
			ts=session.beginTransaction();
			HashSet set=new HashSet();
			if(news.getPicfileFileName()!=null&&news.getPicfileFileName().length>0)
			{
				
				for(int i=0;i<news.getPicfileFileName().length;i++)
				{
				Newspic pic=new Newspic();
				pic.setNewsid(news);
				pic.setNewspath(news.getPicfileFileName()[i]);
				set.add(pic);
				}
				news.setNewspics(set);
			}
			session.save(news);
			ts.commit();
			return "success";
			
		}
		catch(Exception ee)
		{
				ts.rollback();
			System.out.println("Exception ee when news id added");
			ee.printStackTrace();
		}
		finally
		{	
			DAO.closeSession();
		}
		return "error";
		
	}
	public News getNewsById(Integer id)
	{
		
		News news=null;
		Transaction ts=null;
		Session session=DAO.openSession();
		try
		{
			
			ts=session.beginTransaction();
			news=(News)session.get(News.class, id);
			ts.commit();
		
			
		}
		catch(Exception ee)
		{
			System.out.println("Exception ee when get news by id");
			ee.printStackTrace();
		}
		finally
		{
			DAO.closeSession();
		}
		return news;
	}
	public List<News> getAllNew(int page,int type,int size)
	{
		List<News> list=null;
		Session session=DAO.openSession();
		Transaction ts=null;
		try
		{
			ts=session.beginTransaction();
			
			Query query=null;
			if(type!=0)
			{
				query=session.createQuery("from News as a where a.newstype=:type order by newsdate desc");
				query.setInteger("type",type);
			}
			else
				query=session.createQuery("from News");
			
			query.setFirstResult(size*(page-1));
			query.setMaxResults(size);
			list=(List<News>)query.list();
			ts.commit();
			session.close();
		}
		catch(Exception ee)
		{
			System.out.println("Exception when get new page");
			ee.printStackTrace();
		}
		finally
		{
			DAO.closeSession();
		}
		
		return list;
	}
	public String deleteNews(String[] newids,String path)
	{
		Session session=DAO.openSession();
		Transaction ts=null;
		try
		{	
			File file=null;
			News news=null;
			ts=session.beginTransaction();
			for(int i=0;i<newids.length;i++)
			{
					
				news=(News)session.get(News.class, Integer.valueOf(newids[i]));
				if(!news.getNewspics().isEmpty())
				{
					for(Iterator<Newspic> it=news.getNewspics().iterator();it.hasNext();)
					{
						file=new File(path,it.next().getNewspath());
						if(file.exists()) file.delete();
					}
				}
				session.delete(news);
			}
			ts.commit();
		}
		catch(Exception ee)
		{
			System.out.println("Exception ee when delete news");
			ee.printStackTrace();
			return "false";
		}
		finally
		{
			DAO.closeSession();
		}
		return "true";
	}
	public String modifyNews(News news,String path)
	{
		Session session=DAO.openSession();
		Transaction ts=null;
		try
		{
			ts=session.beginTransaction();
			News nw=this.getNewsById(news.getId());
			nw.setNewscontent(news.getNewscontent());
			if(news.getPicfileFileName()!=null&&news.getPicfileFileName().length>0)
			{
				if(nw.getNewspics()!=null&&nw.getNewspics().size()>0)
				{
					//���ԭ�е�ͼƬ
					nw.getNewspics().clear();
					//ֻ�ǰ�ֵ�޸ĳ�NULL����� ����ѧϰ
					//ɾ��ͼƬ
					this.detelePic(path, news.getPicfileFileName());
					for(int i=0;i<news.getPicfileFileName().length;i++)
					{
					
					Newspic pic=new Newspic();
					pic.setNewspath(news.getPicfileFileName()[i]);
					pic.setNewsid(nw);
					nw.addTonewspics(pic);
					}
				}
				
				
			}
			nw.setNewstitle(news.getNewstitle());
			session.update(nw);
			ts.commit();

		}
		catch(Exception ee)
		{
			ts.rollback();
			System.out.println("Exception when modify news");
			ee.printStackTrace();
		}
		finally
		{
			DAO.closeSession();
		}
		return "success";
	}
	private void detelePic(String path,String[] filepath)
	{
		File file=null;
		for(int i=0;i<filepath.length;i++)
		{
			file=new File(path,filepath[i]);
			System.out.println(file.exists());
			if(file.exists())
				file.delete();
		}
	}
	public int getNewsAccount(int type)
	{
		String sql="select count(*) from news where newstype="+type;
		return DAO.getAccount(sql);
	}
	public List<News> getAllNew(String id,int type,int size)
	{
		List<News> list=null;
		Session session=DAO.openSession();
		Transaction ts=null;
		try
		{
			ts=session.beginTransaction();
			
			Query query=null;
			if(type!=0)
			{
				query=session.createQuery("from News as a where a.newstype=:type and a.id>:id order by id asc");
				query.setInteger("type",type);
				query.setInteger("id",Integer.valueOf(id));
			}
			else
				query=session.createQuery("from News");
			
			query.setFirstResult(0);
			query.setMaxResults(5);
			list=(List<News>)query.list();
			ts.commit();
			session.close();
		}
		catch(Exception ee)
		{
			System.out.println("Exception when get new page");
			ee.printStackTrace();
		}
		finally
		{
			DAO.closeSession();
		}
		
		return list;
	}

}
